<template >
  <div class="content">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="账号" prop="account">
        <el-input
          type="text"
          v-model="ruleForm.account"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="ruleForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input type="text" v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model.number="ruleForm.age"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-radio v-model="ruleForm.gender" label="男">男</el-radio>
        <el-radio v-model="ruleForm.gender" label="女">女</el-radio>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input type="text"   v-model.number="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >提交</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("年龄不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("请输入数字值"));
        } else {
          if (value < 18) {
            callback(new Error("必须年满18岁"));
          } else {
         
            
            callback();
          }
        }
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      }else if(value.length<6){
      callback(new Error("密码必须超过6位"));
      }else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        console.log(value);
        
        console.log("密码");
        callback();
      }
    };
    var checkName = (rule, value, callback) => {
      console.log("");
      if (!value) {
        return callback(new Error("姓名不能为空"));
      }
      callback()
    };
    var checkPhone = (rule, value, callback) => {
      let phoneReg = /^[1][3,4,5,7,8][0-9]{9}$/;
      if (!value) {
        return callback(new Error("电话不能为空"));
      }else if (!phoneReg.test(value)) {
        callback(new Error("格式有误！！！"));
      } else {
        callback();
      }
    };
    var checkAccount = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("账号不能为空"));
      }else{
        console.log('账号');
        callback()
      }

    };
    return {
      ruleForm: {
        name: "",
        password: "",
        age: "",
        account: "",
        gender: "男",
        phone: "",
        position:2,
        status:1,
        isBan:false
      },
      rules: {
        password: [{ validator: validatePass, trigger: "blur" }],
        age: [{ validator: checkAge, trigger: "blur" }],
        phone: [{ validator: checkPhone, trigger: "blur" }],
        name: [{ validator: checkName, trigger: "blur" }],
        account: [{ validator: checkAccount, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(ruleForm) {
        this.$refs[ruleForm].validate((valid) => {
          if (valid) {
              this.$store.dispatch("user/register", this.ruleForm);
          } else {
            console.log('error submit!!');
            return false;
          }
        });

    
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style>
.content {
  width: 100%;
  height: 100%;
  background-color:	 #2F4F4F;
  overflow: hidden;
}
.demo-ruleForm {
  width: 500px;
  margin: 40px auto;
  background-color:	#2F4F4F ;
  padding: 40px;
}
</style>